import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import java.sql.*;

import dbAdapter.DatabaseAdapter;
import CommonClass.IMG;
import CommonClass.URLSearch;
import CommonClass.URLlink;
import CommonClass.keyword;
import WebSearch.SearchException;
import WebSearch.WebSearch;
import WebSearch.WebSearchV1;
import WebSearch.WebSearchV2;
import WebSearch.WebSearchV3;
 


public class TestMain {
	
	private static String DATABASE_NAME 			= "imagesearch";
	private	static String KEYWORD_TABLE	 			= "keyword";
	private	static String RESULT_TABLE	     		= "result";
	private	static String SEARCHED_TABLE      		= "searchhistory";
	private	static String KEYWORD_STATISTICS_TABLE	= "keywordstatistics";
	private	static String TEMP_SEARCH_TABLE 		= "tempsearchlist";
	private static String UNPROCESSED_TABLE			= "unprocessed_searchlist";
	private	static String DATABASE_USERNAME 		= "root";
	private	static String DATABASE_PASSWORD 		= "1234";
			
	private	static String KEYWORD;
	private	static boolean isOccupied				= false;
	
			
	private static	Connection connect				= null;
	private	static Statement  stmt 					= null;
	
	
	public static void TestWebSearch() throws Exception{
		// TODO Auto-generated method stub
//		WebSearch ws = new WebSearch();
//		
//		ArrayList<URLlink> urls = ws.getHREFs("http://www.simplehtmlguide.com/");
//		
//		for(int i = 0; i < urls.size(); i++)
//		{
//			if(urls.get(i).getKeywords() != null)
//				System.out.println(urls.get(i).getURL() + " \t =>" + urls.get(i).getKeywords().get(0).getTag() + " " + urls.get(i).getKeywords().get(0).getKeyword());
//			else
//				System.out.println(urls.get(i).getURL());
//		}
//		
//		URLlink url = new URLlink();
//		url.setURL("http://weather.yahoo.com/", "http://www.kshowonline.com/");
//		
//		ArrayList<IMG> imgs = ws.getIMGs("http://weather.yahoo.com/");
//		
//		for(int i = 0; i < imgs.size(); i++)
//		{
//			if(imgs.get(i).getKeywords() != null)
//				System.out.println(imgs.get(i).getIMG() + " \t =>" + imgs.get(i).getKeywords().get(0).getTag() + " " + imgs.get(i).getKeywords().get(0).getKeyword());
//			else
//				System.out.println(imgs.get(i).getIMG());
//		}
		
	}
	
	
	public static void TestWebSearchV1() throws Exception
	{
//		WebSearchV1 ws1 = new WebSearchV1();
//		
//		//URL uRLStr = new URL("google.com");
//		//System.out.println(uRLStr.toString());http://en.wikipedia.org/wiki/Good/
//		URLSearch uRLSearch = ws1.fetchWebPage(ws1.formatDomain("http://www.pokemon.com/us/pokemon-fun-zone/pokemon-online-games/"));
//		try {
//			ArrayList<URLlink> urls = uRLSearch.getListOfURL();
//			
//			for(int i = 0; i < urls.size(); i++)
//			{
//				if(urls.get(i).getKeywords() != null)
//					System.out.println(urls.get(i).getURL() + " \t =>" + urls.get(i).getKeywords().get(0).getTag() + " " + urls.get(i).getKeywords().get(0).getKeyword());
//				else
//					System.out.println(urls.get(i).getURL());
//			}
//			
//			ArrayList<IMG> imgs = uRLSearch.getListOfIMG();
//			System.out.println(imgs.size());
//			for(int i = 0; i < imgs.size(); i++)
//			{
//				if(imgs.get(i).getKeywords() != null)
//					System.out.println(imgs.get(i).getIMG() + " \t =>" + imgs.get(i).getKeywords().get(0).getTag() + " " + imgs.get(i).getKeywords().get(0).getKeyword());
//				else
//					System.out.println(imgs.get(i).getIMG());
//			}
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			
//			e.printStackTrace();
//		}
	}
	
	public static void TestWebSearchV2() throws Exception
	{
//		WebSearchV1 ws1 = new WebSearchV1();
//		
//		//URL uRLStr = new URL("google.com");
//		//System.out.println(uRLStr.toString());http://en.wikipedia.org/wiki/Good/
//		URLSearch uRLSearch = ws1.fetchWebPageWithKeyword(ws1.formatDomain("http://www.pokemon.com/us/pokemon-fun-zone/pokemon-online-games/"), "minigame promo");
//		try {
//			ArrayList<URLlink> urls = uRLSearch.getListOfURL();
//			
//			for(int i = 0; i < urls.size(); i++)
//			{
//				if(urls.get(i).getKeywords() != null)
//					System.out.println(urls.get(i).getURL() + " \t =>" + urls.get(i).getKeywords().get(0).getTag() + " " + urls.get(i).getKeywords().get(0).getKeyword());
//				else
//					System.out.println(urls.get(i).getURL());
//			}
//			
//			ArrayList<IMG> imgs = uRLSearch.getListOfIMG();
//			System.out.println(imgs.size());
//			for(int i = 0; i < imgs.size(); i++)
//			{
//				if(imgs.get(i).getKeywords() != null)
//					System.out.println(imgs.get(i).getIMG() + " \t =>" + imgs.get(i).getKeywords().get(0).getTag() + " " + imgs.get(i).getKeywords().get(0).getKeyword());
//				else
//					System.out.println(imgs.get(i).getIMG());
//			}
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			
//			e.printStackTrace();
//		}
	}
	
	public static void TestWebSearchV3() throws Exception
	{

		WebSearchV2 ws2 = new WebSearchV2();
//OPTION 1 (SAVE with only 1 function)
//		URLSearch uRLSearch = ws2.fetchWebPageWithKeyword(ws2.formatDomain("http://en.wikipedia.org/wiki/Dog"), "dog");
//		ArrayList<IMG> imgs = uRLSearch.getListOfIMG();
//		System.out.println(imgs.size());
//		for(int i = 0; i < imgs.size(); i++)
//		{
//			//if(imgs.get(i).getKeywords() != null)
//			//	System.out.println(imgs.get(i).getIMG() + " \t =>" + imgs.get(i).getKeywords().get(0).getTag() + " " + imgs.get(i).getKeywords().get(0).getKeyword());
//			//else
//				System.out.println("IMG Link:<" + imgs.get(i).getImageURLlink() +">");
//		}
		
//OPTION 2 (TEST on the number of link)
	//	ws2.fetchWebPage(ws2.formatDomain("http://en.wikipedia.org/wiki/Dog"));

//OPTION 3 (Separate link from image method)
		// API FOR Separate 
		String keyword = "dog";
		String[] keyword_array = keyword.split(" ");
		
		dbAdapter.DatabaseAdapter db = ws2.fetchWebPageV1(ws2.formatDomain("http://en.wikipedia.org/wiki/dog"));

		ArrayList<IMG> listOfImg = ws2.getIMGs(keyword_array, keyword_array.length, db);
		System.out.printf("No of Image: %s", listOfImg.size());
		
//		if(listOfImg.size() < 11)
		{
			ws2.getHREFs(keyword_array, keyword_array.length, db);
		}
		
		db.returnMutex();
	}
	public static void TestWebSearchV4() throws Exception
	{
		WebSearchV3 ws3 = new WebSearchV3();
		
		ws3.fetchWebPageV1("http://en.wikipedia.org/wiki/dog");
		String keyword = "dog";
		String[] keyword_array = keyword.split(" ");
		
		ArrayList<IMG> listOfImg = ws3.getIMGs(keyword_array, keyword_array.length, null);
		System.out.printf("No of Image: %s", listOfImg.size());
		System.out.printf("No of Link: %s", ws3.getHREFs(keyword_array, keyword_array.length, null).size());
		
	}
	public static void main(String[] args) throws Exception {
//		TestWebSearch();
//		WebSearchV1 ws1 = new WebSearchV1();
//		System.out.println(ws1.formatDomain("stackoverflow.com/questions/4662215/java-how-to-extract-a-substring-using-regex"));
//		System.out.println(ws1.formatDomain("sg.yahoo.com"));
//		System.out.println(ws1.formatDomain("sg.yaho"));
////		System.out.println(ws1.formatURL("http://www.google.com","index.php"));
//		ws1.fetchWebPage(ws1.formatDomain("http://www.yahoo.com"));//ws1.formatDomain("yahoo.com"));//"https://www.google.com.sg/#q=how+to+take+screenshot");
////		TestWebSearch();
		//TestWebSearchV1();
		//TestWebSearchV2();
		
//		TestWebSearchV3();
//		DatabaseAdapter database = DatabaseAdapter.getInstance();
//		
//		HashMap<URLlink,Integer> mpp= new HashMap<URLlink, Integer>();
//		URLlink link1 = new URLlink("www.google.com");
//		Integer i1 = new Integer(1);
//		URLlink link2 = new URLlink("www.gmail.com");
//		URLlink link3 = new URLlink("www.google.com");
//		URLlink link4 = new URLlink("www.yahoo.com");
//		mpp.put(link1, i1);
//		mpp.put(link2, i1);
//		mpp.put(link3, i1);
//		mpp.put(link4, i1);
//		database.addNewUnprocessedUrlList(mpp);
		TestWebSearchV4();
		
		
		
		
		
	}
	

}
